/*
 * Rhy Auto Complete
 * 
 * @author Tristan Maindron
 * @dependencies rhy.core.wrapper, rhy.core.config, rhy.core.view, rhy.core.ajax
 * 
 */

Rhy.extra.autocomplete = {
    apply: function(field, config){
        
        config = $r.cfg.mgr.merge({
            source: '#',
            field: 'term',
            params: null,
            type: 'json',
            min: 3,
            limit: 10,
            getPropertyToIterate: function(o){
                return o;
            },

            renderField: function(o){
                return o;
            },
            
            matchQuery: function(o, query){
                return true;
            },
            
            onSelect: function(){
                
            }
        }, config);
        
        var div = document.createElement('div');
        div.className = "rhy-ac-container";
        div.style.display = 'none';
        document.body.appendChild(div);
        
        $r.on(field, 'keypress', function(){
           if(field.value.length < config.min){
               return;
           }
           
           if(typeof(config.source) != "string"){
               $r.ex.autocomplete.processData(config.source, field, div, config);
           }
           else{
               var url;
               if(config.field == null)
                    url = config.source + "?" + config.field + "=" + field.value + (config.params != null ? "&" + $r.cfg.mgr.encode(config.params) : "");
               else
                    url = config.source + field.value + (config.params != null ? "?" + $r.cfg.mgr.encode(config.params) : "");
               
               Rhy.ajax({
                  url: url,
                  type: config.type,
                  success: $r.ex.autocomplete.callbackGenerator(field, div, config)
               });
           }
        });
        
        $r.on(document.body, 'click', function(e){
            var target = $r.evt.source(e);
            var targetDiv = target.parentNode;
            
            if(targetDiv != null){
                targetDiv = targetDiv.parentNode;
            }
            
            if(target.className.indexOf("rhy-ac-result", 0) != -1 && targetDiv == div){
                field.value = target.innerHTML;
                field.rhyAcData = target.rhyAcData;
                config.onSelect(field);
            }
            
            div.style.display = "none";
        })
        
    },
    
    processData: function(data, field, div, config){
        div.innerHTML = '';
        var ul = document.createElement('ul');
        ul.className = "rhy-ac-content";

        var top = document.createElement('div');
        top.className = 'rhy-ac-top';

        var bottom = document.createElement('div');
        bottom.className = 'rhy-ac-bottom';

        var count = 0;

        for(var i=0; i < data.length && i < config.limit; i++){
            var li = document.createElement('li');
            li.className = "rhy-ac-result";

            if(count%2 ==0){
                li.className += " rhy-ac-result-odd";
            }
            else{
                li.className += " rhy-ac-result-even";
            }
            
            if(config.matchQuery(data[i], field.value)){
                li.innerHTML = config.renderField(data[i]);
                li.rhyAcData = data[i];
                ul.appendChild(li);
                count++;
            }
        }

        div.appendChild(top);
        div.appendChild(ul);
        div.appendChild(bottom);

        if(count > 0){
            div.style.display = 'block';
            div.style.position = 'absolute';
            div.style.top = ($r.vw.gp.getOffsetTop(field) + $r.vw.gp.getOffsetHeight(field)) + 'px';
            div.style.left = $r.vw.gp.getOffsetLeft(field) + 'px';
        }
        else{
            div.style.display = 'none';
        }
    },
    
    callbackGenerator: function(field, div, config){
        return function(o){
            o = config.getPropertyToIterate(o);
            
            $r.ex.autocomplete.processData(o, field, div, config);
        }
    }
}
